<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * color short summary.
 *
 * color description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Color extends Goods_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->Model('Color_model');
        $this->load->library('form_validation');
    }

    public function colorlist()
    {
        $cupge=$this->input->get('page');
        $cupge=empty($cupge) || $cupge=="0"?1:$cupge;

        $pagesize=10;
        $one=$this->Color_model->get_page($cupge,$pagesize,"*","color","order by color_id desc","color_id","");
        $this->load->library('page');
        $p=new Page();
        $data['colorList']=arrayToObject($one['objlist']);
        $p->init($one['count'][0]->_count,5,$cupge,$pagesize,site_url('admin/color/colorlist'));
        $data['links']=$p->showPages(1);
        $this->load->view('Admin/color/colorlist.html',$data);

    }

    public function addcolor()
    {
        $this->load->view('Admin/color/addcolor.html');
    }

    public function add_color()
    {
        $valiRes= $this->form_validation->run('color_vali');
        $this->load->library('ajaxMsgModel');
        $colorModel= new Color_model();
        if($valiRes)
        {
            $colorModel->title=$this->input->post('title');
            $colorModel->colorkey=$this->input->post('colorkey');
            $res= $colorModel->insert_color($colorModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='添加成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url("admin/color/colorlist");
            }
            else
            {
                $this->ajaxmsgmodel->Msg='添加失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function editcolor()
    {
        $colorId=$this->uri->segment(4);
    	$data['colorModel']=$this->Color_model->get_color_by_id($colorId);
        $this->load->view('Admin/color/editcolor.html',$data);
    }

    public function edit_color()
    {
        $valiRes= $this->form_validation->run('color_vali');
        $this->load->library('ajaxMsgModel');
        $colorModel= new Color_model();
        if($valiRes)
        {
            $colorModel->color_id=$this->input->post('color_id');
            $colorModel->title=$this->input->post('title');
            $colorModel->colorkey=$this->input->post('colorkey');
            $res= $colorModel->edit_color($colorModel);
            if($res)
            {
                $this->ajaxmsgmodel->Msg='修改成功';
                $this->ajaxmsgmodel->Statu='ok';
                $this->ajaxmsgmodel->BackUrl=site_url("admin/color/colorlist");
            }
            else
            {
                $this->ajaxmsgmodel->Msg='修改失败';
                $this->ajaxmsgmodel->Statu='err';
            }
        }
        else{
            $errors=  $this->form_validation->error_array();
            $this->ajaxmsgmodel->Msg='服务器验证失败:'.get_validate_errors($errors);;
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }



    public function del_color()
    {
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $res=$this->Color_model->del_color_byid($this->uri->segment(4));
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }

    public function batchdel_color()
    {
        $color_id=$this->input->post('items');
        $this->load->library('ajaxMsgModel');
        $backurl=$this->input->get('returnurl');
        $colorModel=new $this->Color_model();
        $color_idArray=explode('|',trim($color_id,'|'));
        $res=0;
        if(count($color_idArray)>0)
        {
            $res=$colorModel->batchdel_color($color_idArray);
        }
        if($res>0)
        {
            $this->ajaxmsgmodel->Msg='操作成功';
            $this->ajaxmsgmodel->Statu='ok';
            $this->ajaxmsgmodel->BackUrl=$backurl;
        }
        else
        {
            $this->ajaxmsgmodel->Msg='操作失败';
            $this->ajaxmsgmodel->Statu='err';
        }
        echo(json_encode($this->ajaxmsgmodel));
    }
}